package com.apex.aolonfitdemo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.apex.aolonfit.AolonFitManager;
import com.apex.aolonfitdemo.R;
import com.apex.aolonfitdemo.databinding.ActivityMinHeartBinding;
import com.apex.aolonfitdemo.dialog.MonthDialog;
import com.apex.aolonfitdemo.dialog.SwitchDialog;
import com.apex.aolonfitdemo.dialog.WaitingDialog;
import com.apex.ax_bluetooth.core.EABleManager;
import com.apex.ax_bluetooth.enumeration.EABleConnectState;
import com.cyber.bridge.module.bean.HeartRateSetEntity;
import com.cyber.bridge.module.listener.OnCyberHeartRateConfigListener;

public class MinHeartActivity extends AppCompatActivity {
    ActivityMinHeartBinding activityMinHeartBinding;
    WaitingDialog waitingDialog;
    private SwitchDialog heartDialog;
    private MonthDialog monthDialog;
    int heartRate;
    String tempHeart;
    Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                HeartRateSetEntity heartRateSetEntity = (HeartRateSetEntity) msg.obj;
                if (heartRateSetEntity == null) {
                    return;
                }
                heartRate = heartRateSetEntity.getValue();
                activityMinHeartBinding.minHeartRate.setText(heartRate + "");
                if (!heartRateSetEntity.isEnabled()) {
                    activityMinHeartBinding.alarmSwitch.setText(getString(R.string.switch_state_close));
                    tempHeart = getString(R.string.switch_state_close);
                } else {
                    tempHeart = getString(R.string.switch_state_on);
                    activityMinHeartBinding.alarmSwitch.setText(getString(R.string.switch_state_on));

                }
            } else if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                activityMinHeartBinding.minHeartRate.setText(heartRate + "");
                activityMinHeartBinding.alarmSwitch.setText(tempHeart);
            } else if (msg.what == 0x43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(MinHeartActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityMinHeartBinding = ActivityMinHeartBinding.inflate(getLayoutInflater());
        setContentView(activityMinHeartBinding.getRoot());
        activityMinHeartBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityMinHeartBinding.tool);
        getSupportActionBar().setTitle(null);
        activityMinHeartBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        AolonFitManager.getInstance().setOnHeartRateConfigListener(new OnCyberHeartRateConfigListener() {
            @Override
            public void queryHeartRateAutoMeasure(@Nullable HeartRateSetEntity heartRateSetEntity) {

            }

            @Override
            public void setHeartRateAutoMeasure(boolean b) {

            }

            @Override
            public void queryHeartRateHighWarning(@Nullable HeartRateSetEntity heartRateSetEntity) {

            }

            @Override
            public void setHeartRateHighWarning(boolean b) {

            }

            @Override
            public void queryHeartRateLowWarning(@Nullable HeartRateSetEntity heartRateSetEntity) {
                if (mHandler != null) {
                    Message message = mHandler.obtainMessage();
                    message.what = 0x40;
                    message.obj = heartRateSetEntity;
                    mHandler.sendMessage(message);
                }

            }

            @Override
            public void setHeartRateLowWarning(boolean b) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(b ? 0x42 : 0x43);
                }
            }
        });
        if (waitingDialog == null) {
            waitingDialog = new WaitingDialog(MinHeartActivity.this);
        }
        waitingDialog.show();
        AolonFitManager.getInstance().queryHeartRateLowWarning();
        activityMinHeartBinding.alarmSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (heartDialog == null) {
                    heartDialog = new SwitchDialog(MinHeartActivity.this);
                    heartDialog.setSelectListener(new SwitchDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            tempHeart = sex;
                            if (waitingDialog == null) {
                                waitingDialog = new WaitingDialog(MinHeartActivity.this);
                            }
                            if (!waitingDialog.isShowing()) {
                                waitingDialog.show();
                            }

                            if (sex.equalsIgnoreCase(getString(R.string.switch_state_on))) {
                                AolonFitManager.getInstance().setHeartRateLowWarning(new HeartRateSetEntity(true, heartRate));
                            } else {
                                AolonFitManager.getInstance().setHeartRateLowWarning(new HeartRateSetEntity(false, heartRate));
                            }
                        }
                    });
                }
                if (!heartDialog.isShowing()) {
                    heartDialog.show();
                }

            }
        });
        activityMinHeartBinding.minHeartRate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (monthDialog == null) {
                    monthDialog = new MonthDialog(MinHeartActivity.this, getString(R.string.min_heart_rate));
                    monthDialog.setSelectListener(new MonthDialog.SelectListener() {
                        @Override
                        public void selectData(int sex) {
                            heartRate = sex;
                            if (waitingDialog == null) {
                                waitingDialog = new WaitingDialog(MinHeartActivity.this);
                            }
                            if (!waitingDialog.isShowing()) {
                                waitingDialog.show();
                            }
                            AolonFitManager.getInstance().setHeartRateLowWarning(new HeartRateSetEntity(getString(R.string.switch_state_close).equalsIgnoreCase(tempHeart) ? false : true, heartRate));
                        }
                    });
                }
                if (!monthDialog.isShowing()) {
                    monthDialog.show();
                }

            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (heartDialog != null) {
            heartDialog.dismiss();
            heartDialog.destroyDialog();
            heartDialog = null;
        }
        if (monthDialog != null) {
            monthDialog.dismiss();
            monthDialog.destroyDialog();
            monthDialog = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        super.onDestroy();
    }
}
